/*
* Copyright (C) 2012 Manuel Pérez
*
* This file is part of OpenSudoku.
*
* OpenSudoku is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenSudoku is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenSudoku. If not, see <http://www.gnu.org/licenses/>.
*
*/
package igraf.bezeroa;

import igraf.ItzuliInterfazea;
import igraf.Panela;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.text.MaskFormatter;

import dBasea.Bezeroa;

public class AltaBezeroa extends Panela{
	private static final long serialVersionUID = -7945835312048770318L;
	
	private JButton ok;
	private JButton ezeztatu;
	private JLabel izena;
	private JTextField ize;
	private JLabel abizenak;
	private JTextField abi;
	private JLabel helbidea;
	private JTextField helb;
	private JLabel telefonoa;
	private JTextField tele;
	private JLabel seguru;
	private JRadioButton bai;
	private JRadioButton ez;
	private ButtonGroup baiez;
	private JTextField nif;
	private JLabel bezNif;
	private Bezeroa beze=new Bezeroa();
	
	public AltaBezeroa(String tituloa, final ItzuliInterfazea itzuliInterfazea) {		
		super(tituloa);
		
		JPanel erdikoPanela= this.getErdikoPanela();
		erdikoPanela.setBackground(SystemColor.activeCaption);
		erdikoPanela.setLayout(new BorderLayout(0, 0));

		JPanel erdikoa = new JPanel();
		erdikoa.setBackground(SystemColor.activeCaption);
		
		JPanel xxx=new JPanel();
		xxx.setBackground(SystemColor.activeCaption);
		erdikoa.add(xxx);
		
		JPanel xx=new JPanel();
		xx.setBackground(SystemColor.activeCaption);
		bezNif=new JLabel("Bezero NIF:");
		bezNif.setFont(new Font("Arial", Font.PLAIN, 14));
		nif=new JTextField(20);
		nif.addKeyListener(new KeyAdapter() {//limitea NAN
			
			public void keyTyped(KeyEvent arg0) {
				if (nif.getText().length()>=9){
					arg0.consume();
					}
			}
		});
		xx.add(bezNif);
		xx.add(nif);
		erdikoa.add(xx);
		
		erdikoa.add(xx);
		
		JPanel arrib = new JPanel();
		arrib.setBackground(SystemColor.activeCaption);
		FlowLayout fl_arrib = new FlowLayout();
		fl_arrib.setHgap(15);
		fl_arrib.setVgap(9);
		arrib.setLayout(fl_arrib);
		izena=new JLabel("Izena:");
		izena.setFont(new Font("Arial", Font.PLAIN, 14));
		ize=new JTextField(20);
		arrib.add(izena);
		arrib.add(ize);
		erdikoa.add(arrib);
		
		JPanel erdi = new JPanel();
		erdi.setBackground(SystemColor.activeCaption);
		FlowLayout fl_erdi = new FlowLayout();
		fl_erdi.setHgap(15);
		fl_erdi.setVgap(8);
		erdi.setLayout(fl_erdi);
		abizenak=new JLabel("Abizenak:");
		abizenak.setFont(new Font("Arial", Font.PLAIN, 14));
		abi=new JTextField(20);
		erdi.add(abizenak);
		erdi.add(abi);
		erdikoa.add(erdi);
		
		JPanel lau = new JPanel();
		lau.setBackground(SystemColor.activeCaption);
		FlowLayout fl_lau = new FlowLayout();
		fl_lau.setHgap(15);
		fl_lau.setVgap(7);
		lau.setLayout(fl_lau);
		helbidea=new JLabel("Helbidea:");
		helbidea.setFont(new Font("Arial", Font.PLAIN, 14));
		helb=new JTextField(20);
		lau.add(helbidea);
		lau.add(helb);
		erdikoa.add(lau);
		
		JPanel bost = new JPanel();
		bost.setBackground(SystemColor.activeCaption);
		FlowLayout fl_bost = new FlowLayout();
		fl_bost.setHgap(15);
		fl_bost.setVgap(8);
		bost.setLayout(fl_bost);
		telefonoa=new JLabel("Telefonoa:");
		telefonoa.setFont(new Font("Arial", Font.PLAIN, 14));
		telefonoa.setVerticalAlignment(SwingConstants.BOTTOM);
		try {
			tele=new JFormattedTextField(new MaskFormatter("#########"));//zenbakizko limitea telefonorako
			tele.setColumns(12);

		} catch (ParseException e2) {
			e2.printStackTrace();
		}
		bost.add(telefonoa);
		bost.add(tele);
		erdikoa.add(bost);
		
		JPanel sei = new JPanel();
		sei.setBackground(SystemColor.activeCaption);
		bost.setBackground(SystemColor.activeCaption);
		FlowLayout fl_sei = new FlowLayout();
		fl_sei.setHgap(15);
		fl_sei.setVgap(8);
		sei.setLayout(fl_sei);
		seguru=new JLabel("Aseguru mota:");
		seguru.setHorizontalAlignment(SwingConstants.CENTER);
		seguru.setFont(new Font("Arial", Font.PLAIN, 14));
		
		
		sei.add(seguru);
			
		erdikoa.add(sei);
		
		erdikoPanela.add(erdikoa, BorderLayout.CENTER);
		erdikoa.setLayout(new GridLayout(8,1));
		JPanel gs=new JPanel();
		gs.setBackground(SystemColor.activeCaption);
		erdikoa.add(gs);
		
		try {
			//kargatu datu basetik izenak eta radiobutonean jarri
			List<String> aseguruTipoak = beze.getAseguruTipoDeskripzioa();
			bai=new JRadioButton(aseguruTipoak.get(0), true);
			bai.setFont(new Font("Arial", Font.PLAIN, 14));
			ez=new JRadioButton(aseguruTipoak.get(1));
			ez.setFont(new Font("Arial", Font.PLAIN, 14));
			baiez= new ButtonGroup();
			baiez.add(bai);
			baiez.add(ez);
			gs.add(bai);
			gs.add(ez);
		}
		catch (SQLException e1) {
			e1.printStackTrace();
		}
		
		FlowLayout fl_behekoa = new FlowLayout();
		fl_behekoa.setVgap(20);
		fl_behekoa.setHgap(110);
		JPanel behekoa = new JPanel(fl_behekoa);
		behekoa.setBackground(SystemColor.activeCaption);
		ok = new JButton("Aldaketak ezarri");
		ok.setHorizontalAlignment(SwingConstants.LEFT);
		ok.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ok);
		ezeztatu = new JButton("Ezeztatu");
		
		ezeztatu.setHorizontalAlignment(SwingConstants.RIGHT);
		ezeztatu.setFont(new Font("Arial", Font.PLAIN, 14));
		behekoa.add(ezeztatu);
		erdikoPanela.add(behekoa, BorderLayout.SOUTH);			
		
		ok.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					if (!zerbaiIdatzi()) {
						JOptionPane.showMessageDialog(null,
								"Sartu behar duzu zerbait zuriunetan\n"
										+ "Ezin da eremu hutsik egon", "Oker",
								JOptionPane.ERROR_MESSAGE);
					} else if (beze.existitzenDa(nif.getText())) {
						JOptionPane.showMessageDialog(null,
								"Bezero hau existitzen da\n"
										+ "Sartu NAN berria duen pertsona",
								"Oker", JOptionPane.ERROR_MESSAGE);
					}

					else {
						try {
							int tfnoa = Integer.parseInt(tele.getText());
							String balioa = null;
							if (bai.isSelected()) {
								balioa = bai.getText();
							}
							else if (ez.isSelected()) {
								balioa = ez.getText();
							}
							beze.bezSortu(nif.getText(), ize.getText(), abi
									.getText(), helb.getText(), tfnoa, balioa);
							JOptionPane.showMessageDialog(null,
									"Ondo sortu da bezeroa",
									"", JOptionPane.INFORMATION_MESSAGE);
							eremuakGarbitu();
						}
						catch(NumberFormatException nfe) {
							JOptionPane.showMessageDialog(null,
									"Zenbakiak sartu telefonoko eremuan",
									"Oker", JOptionPane.ERROR_MESSAGE);
						}
					}

				} catch (Exception e) {
					JOptionPane.showMessageDialog(null,
							"Ezin izan da sartu datu basean bezeroa",
							"Oker", JOptionPane.ERROR_MESSAGE);
					e.printStackTrace();
					
				}
			}
		});
		
		ezeztatu.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				itzuliInterfazea.itzuliLehenPanela();
			}
		});
	}
	
	private boolean zerbaiIdatzi() {
		boolean aurki;
		aurki=nif.getText().length() > 0 && ize.getText().length() > 0 && abi.getText().length() > 0 && tele.getText().length() > 0 && helb.getText().length() > 0;
   		return aurki;
   	}

	@Override
	public void eremuakGarbitu() {
		nif.requestFocus();
		nif.setEnabled(true);
		nif.setText("");
		ize.setText("");
		abi.setText("");
		helb.setText("");
		tele.setText("");
		
		bai.setSelected(true);
	}
}